//
//  LCTaskDetailViewController.m
//  lechuang
//
//  Created by Mr_hi on 2023/4/13.
//  Copyright © 2023 Mooyi. All rights reserved.
//

#import "LCTaskDetailViewController.h"
#import "LCTaskModel.h"
#import "LCTaskService.h"
#import "LCTaskDetailModel.h"
#import "LCTaskDetailTableViewCell.h"
#import "LCTaskDetailInfoTableViewCell.h"

@interface LCTaskDetailViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIView *headViwe;

@property (weak, nonatomic) IBOutlet UIView *taskInfoView;

@property (weak, nonatomic) IBOutlet UIImageView *taskIconView;

@property (weak, nonatomic) IBOutlet UILabel *taskNameLb;

@property (weak, nonatomic) IBOutlet UILabel *taskPriceLb;

@property (weak, nonatomic) IBOutlet UIButton *systemTag;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

//报名
@property (weak, nonatomic) IBOutlet UIView *singView;

@property (weak, nonatomic) IBOutlet UIView *sumbitView;


@property (strong, nonatomic)LCTaskDetailModel * taskDetail;

@property (strong, nonatomic)NSMutableDictionary * param;

@property (strong, nonatomic)NSMutableDictionary * cellHeightDict;


@end

@implementation LCTaskDetailViewController

- (IBAction)backAction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    [self getTaskDetailData];
}

- (void)configUI {
    [self.taskIconView sd_setImageWithURL:[LCFunction XWX_URLImg:self.task.appthumb]];
    self.taskNameLb.text = self.task.appname;
    self.taskPriceLb.text = kFORMAT(@"%@元",self.task.appreward);
    [self.systemTag setTitle:self.task.systemchar forState:UIControlStateNormal];
    [self.taskNameLb sizeToFit];
    
    CGFloat nameHeight = self.taskNameLb.mj_h;
    UIView * headerView = self.tableView.tableHeaderView;
    headerView.mj_h = nameHeight + 81;
    self.tableView.tableHeaderView = headerView;
    
    if ([self.task.downloadid integerValue] == 0) {
        self.singView.hidden = NO;
        self.sumbitView.hidden = YES;
    } else {
        self.singView.hidden = YES;
        self.sumbitView.hidden = NO;
    }
    self.tableView.rowHeight = UITableViewAutomaticDimension;
}

-(NSMutableDictionary *)param {
    if (!_param) {
        _param = [NSMutableDictionary dictionary];
    }
    return _param;
}

-(NSMutableDictionary *)cellHeightDict {
    if (!_cellHeightDict) {
        _cellHeightDict = [NSMutableDictionary dictionary];
    }
    return _cellHeightDict;
}

- (void)getTaskDetailData {
    [LCTaskService taskDetailWithAppId:self.task.appid LaunchId:self.task.launchid Block:^(BOOL isSuccess, LCTaskDetailModel *taskDetail) {
        if (isSuccess) {
            self.taskDetail = taskDetail;
            if ([self.taskDetail.extend.rewardid integerValue]) {
                self.singView.hidden = YES;
                self.sumbitView.hidden = NO;
                self.param[@"RewardID"] = taskDetail.extend.rewardid;
                self.param[@"RewardKey"] = taskDetail.extend.rewardkey;
            } else {
                self.singView.hidden = NO;
                self.sumbitView.hidden = YES;
            }
            [self.tableView reloadData];
        }
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.taskDetail.extend.step.count + 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 2;
    } else {
        LCTaskDetailStepModel * step = self.taskDetail.extend.step[section - 1];
        if ([step.stepmode isEqualToString:@"无操作项"]) {
            return step.imgArr.count + 1;
        } else if ([step.stepmode isEqualToString:@"复制文本"]) {
            return step.imgArr.count + 2;
        } else {
            if ([step.stepcfg.columnmode isEqualToString:@"上传图片"]) {
                if (step.imgArr.count == 0) {
                    return 2;
                } else {
                    return step.imgArr.count + 1;
                }
            } else {
                return step.imgArr.count + 2;
            }
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString * key = kFORMAT(@"%@",indexPath);
    if (self.cellHeightDict[key]) {
        return [self.cellHeightDict[key] floatValue];
    }
    return UITableViewAutomaticDimension;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            LCTaskDetailInfoTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LCTaskDetailInfoTableViewCellRemark"];
            cell.remarkLb.text = self.task.appdescription;
            return cell;
        } else {
            LCTaskDetailInfoTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LCTaskDetailInfoTableViewCell"];
            return cell;
        }
    } else {
        LCTaskDetailStepModel * step = self.taskDetail.extend.step[indexPath.section-1];
        if (indexPath.row == 0) {
            LCTaskDetailTableViewCell * cell = [LCTaskDetailTableViewCell titleCell];
            cell.numLb.text = kFORMAT(@"%ld",indexPath.section);
            cell.titleLb.text = step.textStr;
            return cell;
        } else {
            if ([step.stepmode isEqualToString:@"无操作项"]) {
                LCTaskDetailTableViewCell * cell = [LCTaskDetailTableViewCell imageCell];
                cell.uploadBtn.hidden = YES;
                cell.deleteImgBtn.hidden = YES;
                cell.showImgView.hidden = YES;
                cell.tipImgStr = step.imgArr[indexPath.row - 1];
                cell.parentVC = self;
                return cell;
            } else if ([step.stepmode isEqualToString:@"复制文本"]) {
                if (indexPath.row <=  step.imgArr.count) {
                    LCTaskDetailTableViewCell * cell = [LCTaskDetailTableViewCell imageCell];
                    cell.uploadBtn.hidden = YES;
                    cell.deleteImgBtn.hidden = YES;
                    cell.showImgView.hidden = YES;
                    cell.tipImgStr = step.imgArr[indexPath.row - 1];
                    cell.parentVC = self;
                    return cell;
                } else {
                    LCTaskDetailTableViewCell * cell = [LCTaskDetailTableViewCell copyDataCell];
                    cell.dataLb.text = step.stepcfg.copydata;
                    return cell;
                }
            } else {
                if ([step.stepcfg.columnmode isEqualToString:@"上传图片"]) {
                    LCTaskDetailTableViewCell * cell = [LCTaskDetailTableViewCell imageCell];
                    cell.uploadBtn.hidden = NO;
                    cell.showImgView.hidden = YES;
                    cell.deleteImgBtn.hidden = YES;
                    cell.parentVC = self;
                    if (step.imgArr.count) {
                        cell.tipImgStr = step.imgArr[indexPath.row - 1];
                    }
                    NSString * imgKey = step.stepcfg.columnname;
                    if (step.imgArr.count > 1) {
                        imgKey = kFORMAT(@"%@%ld",step.stepcfg.columnname, indexPath.row);
                    }
                    if (self.param[imgKey]) {
                        cell.showImgStr = self.param[imgKey];
                        cell.uploadBtn.hidden = YES;
                        cell.showImgView.hidden = NO;
                        cell.deleteImgBtn.hidden = NO;
                    }
                    
                    cell.upLoadBlock = ^(NSString *imgUrl, CGFloat cellH) {
                        [self.param setObject:imgUrl forKey:imgKey];
                        [self.cellHeightDict setObject:kFORMAT(@"%.0f",cellH) forKey:kFORMAT(@"%@",indexPath)];
                        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
                    };
                    cell.deleteBlock = ^{
                        [self.param removeObjectForKey:imgKey];
                        [self.cellHeightDict removeObjectForKey :kFORMAT(@"%@",indexPath)];
                        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
                    };
                    return cell;
                } else {
                    if (indexPath.row <=  step.imgArr.count) {
                        LCTaskDetailTableViewCell * cell = [LCTaskDetailTableViewCell imageCell];
                        cell.uploadBtn.hidden = YES;
                        cell.deleteImgBtn.hidden = YES;
                        cell.showImgView.hidden = YES;
                        cell.tipImgStr = step.imgArr[indexPath.row - 1];
                        cell.parentVC = self;
                        return cell;
                    } else {
                        LCTaskDetailTableViewCell * cell = [LCTaskDetailTableViewCell fieldCell];
                        if (step.stepcfg.defatval.length) {
                            cell.contentTfl.placeholder = step.stepcfg.defatval;
                        }
                        if (self.param[step.stepcfg.columnname]) {
                            cell.contentTfl.text = self.param[step.stepcfg.columnname];
                        }
                        cell.changeContentBlock = ^(NSString *text) {
                            if (text.length) {
                                [self.param setObject:text forKey:step.stepcfg.columnname];
                            } else {
                                [self.param removeObjectForKey:step.stepcfg.columnname];
                            }
                        };
                        return cell;
                    }
                }
            }
        }
    }
}

//报名
- (IBAction)signBtnAction:(UIButton *)sender {
    [self showLoading];
    [LCTaskService downloadTaskAppWithAppID:self.task.appid LaunchID:self.task.launchid Block:^(BOOL isSuccess, NSDictionary *field) {
        if (isSuccess) {
            [self showSuccessText:@"报名成功"];
            self.singView.hidden = YES;
            self.sumbitView.hidden = NO;
            self.param[@"RewardID"] = field[@"rewardid"];
            self.param[@"RewardKey"] = field[@"rewardkey"];
        }
    }];
}

//提交
- (IBAction)submitBtnAction:(UIButton *)sender {
    for (LCTaskDetailStepModel * step in self.taskDetail.extend.step) {
        if ([step.stepmode isEqualToString:@"上传数据"]) {
            if ([step.stepcfg.columnmode isEqualToString:@"上传图片"]) {
                NSString * imgKey = step.stepcfg.columnname;
                if (step.imgArr.count > 1) {
                    for (int i = 0; i < step.imgArr.count; i++) {
                        imgKey = kFORMAT(@"%@%d",step.stepcfg.columnname, i+1);
                        if (![self.param objectForKey:imgKey]) {
                            [self showErrorText:kFORMAT(@"请完成%@",step.textStr)];
                            return;
                        }
                    }
                } else {
                    if (![self.param objectForKey:imgKey]) {
                        [self showErrorText:kFORMAT(@"请完成%@",step.textStr)];
                        return;
                    }
                }
            } else {
                if (![self.param objectForKey:step.stepcfg.columnname]) {
                    [self showErrorText:kFORMAT(@"请完成%@",step.textStr)];
                    return;
                }
            }
        }
    }
    
    [self submitTask];
}

- (void)submitTask {
    [LCTaskService submitTaskWithParam:self.param Block:^(BOOL isSuccess) {
        if (isSuccess) {
            [self showSuccessText:@"提交成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}

@end
